package com.lungle.warmdiary.app.content.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.lungle.warmdiary.app.AppConstant;
import com.lungle.warmdiary.app.content.db.helper.CacheSQLiteOpenHelper;
import com.lungle.warmdiary.app.model.response.MediaAccount;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by God on 2016/8/27.
 */
public class MediaAccountDAO extends BaseDAO<MediaAccount> {
    private CacheSQLiteOpenHelper helper;
    private String DB_NAME = AppConstant.CACHE_DB;


    public MediaAccountDAO(Context context) {
        this.helper = new CacheSQLiteOpenHelper(context, DB_NAME);
    }

    public boolean insert(MediaAccount mediaAccount) {
        db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("mid", mediaAccount.getId());
        values.put("name", mediaAccount.getName());
        values.put("platform", mediaAccount.getPlatform());
        values.put("username", mediaAccount.getUsername());
        values.put("logo", mediaAccount.getLogo());
        values.put("status", mediaAccount.getStatus());
        long result = db.insert("T_my_media_accounts", null, values);
        close();
        return result == -1 ? false : true;
    }


    public int insert(List<MediaAccount> mediaAccounts) {
        db = helper.getWritableDatabase();
        int n = 0;
        db.beginTransaction();
        ContentValues values = null;
        for (MediaAccount mediaAccount : mediaAccounts) {
            values = new ContentValues();
            values.put("mid", mediaAccount.getId());
            values.put("name", mediaAccount.getName());
            values.put("platform", mediaAccount.getPlatform());
            values.put("username", mediaAccount.getUsername());
            values.put("logo", mediaAccount.getLogo());
            values.put("status", mediaAccount.getStatus());
            long result = db.insert("T_my_media_accounts", null, values);
            if (result != -1) {
                n++;
            }
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        return n;
    }


    public void removeAll() {
        db = helper.getWritableDatabase();
        db.delete("T_my_media_accounts", null, null);
        db.endTransaction();
    }


    public List<MediaAccount> find(int platform) {
        db = helper.getReadableDatabase();
        List<MediaAccount> mediaAccounts = new ArrayList<>();
        MediaAccount mediaAccount = null;
        Cursor cursor = null;
        try {
            cursor = db.rawQuery("select * from T_my_media_accounts where platform=?", new String[]{platform + ""});
            while (cursor.moveToNext()) {
                mediaAccount = new MediaAccount();
                mediaAccount.setId(cursor.getString(cursor.getColumnIndex("mid")));
                mediaAccount.setName(cursor.getString(cursor.getColumnIndex("name")));
                mediaAccount.setPlatform(cursor.getInt(cursor.getColumnIndex("platform")));
                mediaAccount.setUsername(cursor.getString(cursor.getColumnIndex("username")));
                mediaAccount.setLogo(cursor.getString(cursor.getColumnIndex("logo")));
                mediaAccount.setStatus(cursor.getInt(cursor.getColumnIndex("status")));
                mediaAccounts.add(mediaAccount);
            }
        } catch (Exception e) {
            e.printStackTrace();
            mediaAccounts = Collections.emptyList();
        } finally {
            if (cursor != null) cursor.close();
        }

        return mediaAccounts;
    }


    public List<MediaAccount> findAll() {
        db = helper.getReadableDatabase();
        List<MediaAccount> mediaAccounts = new ArrayList<>();
        MediaAccount mediaAccount = null;
        Cursor cursor = null;
        try {
            cursor = db.rawQuery("select * from T_my_media_accounts", null);
            while (cursor.moveToNext()) {
                mediaAccount.setId(cursor.getString(cursor.getColumnIndex("id")));
                mediaAccount.setName(cursor.getString(cursor.getColumnIndex("name")));
                mediaAccount.setPlatform(cursor.getInt(cursor.getColumnIndex("platform")));
                mediaAccount.setUsername(cursor.getString(cursor.getColumnIndex("username")));
                mediaAccount.setLogo(cursor.getString(cursor.getColumnIndex("logo")));
                mediaAccount.setStatus(cursor.getInt(cursor.getColumnIndex("status")));
                mediaAccounts.add(mediaAccount);
            }
        } catch (Exception e) {
            e.printStackTrace();
            mediaAccounts = Collections.emptyList();
        } finally {
            if (cursor != null) cursor.close();
        }

        return mediaAccounts;
    }
}
